<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/5/21
 * Time: 2:54 下午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;

class Gongying extends Base
{
    public function index(){
        $list = Db::name("gongying")->where("if_del",0)->paginate(10);
        $this->assign("list",$list);
        $this->assign("page",$list->render());
        return $this->fetch();
    }
    public function add(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data["status"] = 1;
            $data["if_del"] = 0;
            $res = Db::name("gongying")->insert($data);
            if($res){
                return json(["code"=>1,'msg'=>"添加完成"]);
            }
        }
        return $this->fetch();
    }
    public function edit(){
        $id = input("id");
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data["status"] = 1;
            $data["if_del"] = 0;
            $res = Db::name("gongying")->where("id",$id)->update($data);
            if($res){
                return json(["code"=>1,'msg'=>"添加完成"]);
            }
        }
        $data = Db::name("gongying")->where("id",$id)->find();
        $this->assign("data",$data);
        return $this->fetch();
    }
    public function changestatus(){
        $id = input("id");
        $status = input("status");
        if($status==1){
            $status = 0;
        }else{
            $status = 1;
        }
        $res = Db::name("gongying")->where("id",$id)->update(['status'=>$status]);
        if($res){
            return json(["code"=>1,"msg"=>"变更成功"]);
        }
        return json(["code"=>0,"msg"=>"网络延时,稍后再试"]);
    }
    public function del(){
        $id = input("id");

        $res = Db::name("gongying")->where("id",$id)->update(['if_del'=>1]);

        if($res){
            return json(["code"=>1,"msg"=>"删除成功"]);
        }
        return json(["code"=>0,"msg"=>"网络延时,稍后再试"]);
    }
}